﻿namespace MicroCloud.SMS
{
    /// <summary>
    /// <see cref="IConfiguration"/> 扩展方法
    /// </summary>
    public static class ConfigurationExtensions
    {
        #region "扩展方法"
        #region "从 <see cref="IConfiguration"/> 创建 <see cref="SmsOptions"/>"
        /// <summary>
        /// 从 <see cref="IConfiguration"/> 创建 <see cref="SmsOptions"/>
        /// </summary>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public static SmsOptions GetSmsOptions(this IConfiguration configuration)
        {
            var options = configuration.GetOptions<SmsOptions>("MicroCloud:SMS", (conf, key) =>
            {
                IConfigurationSection section = configuration.GetSection(key);
                if (!section.Exists())
                {
                    throw new Exception(I18N.T($"短信选项不存在，请配置 {key} 节点"));
                }

                var opts = section.Get<SmsOptions>();
                opts.Provider = configuration["MicroCloud:SMS:Provider"] ?? "";
                if (opts.Provider.IsMissing())
                {
                    throw new Exception(I18N.T("配置文件中 SMS 节点的 Provider 不能为空"));
                }
                opts.Id = configuration["MicroCloud:SMS:Id"];
                if (opts.Id.IsMissing())
                {
                    throw new Exception(I18N.T("配置文件中 SMS 节点的 Id 不能为空"));
                }
                opts.Secret = configuration["MicroCloud:SMS:Secret"];
                if (opts.Secret.IsMissing())
                {
                    throw new Exception(I18N.T("配置文件中 SMS 节点的 Secret 不能为空"));
                }

                opts.SignName = configuration["MicroCloud:SMS:SignName"];
                opts.VerifyCodeTemplate = configuration["MicroCloud:SMS:VerifyCodeTemplate"];
                opts.VerifyCodeTempParam = configuration["MicroCloud:SMS:VerifyCodeTempParam"];
                opts.VerifyCodeSMS = configuration["MicroCloud:SMS:VerifyCodeSMS"];
                opts.VerifyCodeSMS = opts.VerifyCodeSMS.IsMissing() ? "尊敬的用户您好，您的验证码：{0}，{1}分钟有效，请及时操作，祝您生活愉快！" : opts.VerifyCodeSMS;
                opts.NoticeTemplate = configuration["MicroCloud:SMS:NoticeTemplate"];
                opts.NoticeTempParam = configuration["MicroCloud:SMS:NoticeTempParam"];
                opts.PromoteTemplate = configuration["MicroCloud:SMS:PromoteTemplate"];

                decimal verifyCodeExpireMins = configuration["MicroCloud:SMS:VerifyCodeExpireMins"].CastTo(SmsConstants.DefaultVerifyCodeExpireMins);
                int limitOfNumberDay = configuration["MicroCloud:SMS:LimitOfNumberDay"].CastTo(SmsConstants.DefaultLimitOfNumberDay);
                int storageDays = configuration["MicroCloud:SMS:StorageDays"].CastTo(SmsConstants.DefaultStorageDays);
                opts.VerifyCodeExpireMins = (verifyCodeExpireMins > 1 && verifyCodeExpireMins <= 1440) ? verifyCodeExpireMins : SmsConstants.DefaultVerifyCodeExpireMins;
                opts.LimitOfNumberDay = limitOfNumberDay > 0 ? limitOfNumberDay : SmsConstants.DefaultLimitOfNumberDay;
                opts.StorageDays = storageDays > 0 ? storageDays : SmsConstants.DefaultStorageDays;

                return opts;
            });

            return options;
        }
        #endregion

        #endregion

    }

}
